﻿using LPLS.Commons.Enums;
using LPLS.DBEntities.ConfigModels.AlarmModels.AlarmVarableCodeRuleConfigModels;
using LPLS.DBEntities.ConfigModels.DeviceInfoModels;
using LPLS.DBInterface.ConfigModels.DeviceInfoModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceBaseInfoConfigModule.ViewModels
{
    /// <summary>
    /// 设备基础信息配置视图模型
    /// </summary>
    public class DeviceBaseInfoViewModel : BindableBase
    {



        private ObservableCollection<DeviceBaseInfo> _DeviceBaseList = new ObservableCollection<DeviceBaseInfo>();
        /// <summary>
        /// 设备主要信息列表
        /// </summary>
        public ObservableCollection<DeviceBaseInfo> DeviceBaseList
        {
            get { return _DeviceBaseList; }
            set
            {
                SetProperty(ref _DeviceBaseList, value);

            }
        }


        private DeviceBaseInfo _GridSelectModel;

        public DeviceBaseInfo GridSelectModel
        {
            get { return _GridSelectModel; }
            set
            {
                SetProperty(ref _GridSelectModel, value);

                // 强制通知视图属性已更改

                this.DeviceName = GridSelectModel.DeviceName;


                this.DeviceDescription = GridSelectModel.DeviceDescription;


                this.ComboxSelectIndex = DeviceTypes.IndexOf(GridSelectModel.DeviceTypeName);

                this.SelectDeviceTypeDesc = DeviceTypes[ComboxSelectIndex];

                RaisePropertyChanged(nameof(DeviceName));
                RaisePropertyChanged(nameof(DeviceDescription));
                RaisePropertyChanged(nameof(ComboxSelectIndex));

                RaisePropertyChanged(nameof(SelectDeviceTypeDesc));
            }
        }


        private int _ComboxSelectIndex;

        public int ComboxSelectIndex
        {
            get { return _ComboxSelectIndex; }
            set
            {

                SetProperty(ref _ComboxSelectIndex, value);

            }
        }


        private ObservableCollection<string> _DeviceTypes = new ObservableCollection<string>();
        /// <summary>
        /// combox中的设备类型列表
        /// </summary>
        public ObservableCollection<string> DeviceTypes
        {
            get { return _DeviceTypes; }
            set
            {

                SetProperty(ref _DeviceTypes, value);

            }
        }

        private string _SelectDeviceTypeDesc;

        /// <summary>
        /// combox 中选中的设备类型描述
        /// </summary>
        public string SelectDeviceTypeDesc
        {
            get { return _SelectDeviceTypeDesc; }
            set
            {
                SetProperty(ref _SelectDeviceTypeDesc, value);

            }
        }


        /// <summary>
        /// 加载设备类型数据
        /// </summary>
        private void InitData()
        {
            DeviceBaseList = new ObservableCollection<DeviceBaseInfo>();
            DeviceTypes = new ObservableCollection<string>();

            DeviceBaseList=new ObservableCollection<DeviceBaseInfo>( _deviceBaseInfoService.GetData());
            DeviceInfoConfigEvent.PublishDeviceListChanged(DeviceBaseList.ToList());
            LoadComboBoxData();


        }

     

        private void LoadComboBoxData()
        {
            var desdata = DeviceTypeEnumExtensions.GetAllDeviceTypeDescriptions();
            DeviceTypes = new ObservableCollection<string>(desdata);

        }

        private readonly IDeviceBaseInfoService _deviceBaseInfoService;
        public DeviceBaseInfoViewModel(IDeviceBaseInfoService deviceBaseInfoService)
        {
            _deviceBaseInfoService = deviceBaseInfoService;




            InitData();
        


            AddCommand = new DelegateCommand(AddDeviceBaseInfo);
            DeleteCommand = new DelegateCommand(DeleteDeviceBaseInfo);
            UpdateCommand = new DelegateCommand(UpdateDeviceBaseInfo);
        }


        private string _DeviceName;
        /// <summary>
        /// 设备名称
        /// </summary>
        public string DeviceName
        {
            get { return _DeviceName; }
            set
            {

                SetProperty(ref _DeviceName, value);
            }
        }

        private string _DeviceDescription;
        /// <summary>
        /// 设备描述
        /// </summary>
        public string DeviceDescription
        {
            get { return _DeviceDescription; }
            set
            {
                SetProperty(ref _DeviceDescription, value);
            }
        }



        private string _TipMessage;
        /// <summary>
        /// 提示信息
        /// </summary>
        public string TipMessage
        {
            get { return _TipMessage; }
            set
            {

                SetProperty(ref _TipMessage, value);
            }
        }

        public DelegateCommand AddCommand { get; }

        public DelegateCommand DeleteCommand { get; }
        public DelegateCommand UpdateCommand { get; }


        

        /// <summary>
        /// 添加设备基础信息的命令
        /// </summary>
        private void AddDeviceBaseInfo()
        {
            // 添加设备基础信息的逻辑
            if (string.IsNullOrEmpty(DeviceName) || string.IsNullOrEmpty(DeviceDescription))
            {
                TipMessage = "设备名称和描述不能为空";
                return;
            }
            var newDevice = new DeviceBaseInfo
            {

                DeviceName = DeviceName,
                DeviceType = DeviceTypeEnumExtensions.GetDeviceTypeByDescription(SelectDeviceTypeDesc),
                DeviceTypeName = SelectDeviceTypeDesc,
                DeviceDescription = DeviceDescription
            };
            var i = _deviceBaseInfoService.AddData(newDevice);
            if (i <= 0)
            {
                TipMessage = "设备添加失败";
                return;
            }
            InitData();
         //   DeviceBaseList.Add(newDevice);
            TipMessage = "设备添加成功";
        }


        private void DeleteDeviceBaseInfo()
        {
            // 删除设备基础信息的逻辑
            if (GridSelectModel == null)
            {
                TipMessage = "请选择要删除的设备";
                return;
            }
            var i = _deviceBaseInfoService.DeleteData(GridSelectModel);
            if (i <= 0)
            {
                TipMessage = "设备删除失败";
                return;
            }
            InitData();
            TipMessage = "设备删除成功";
        }

        private void UpdateDeviceBaseInfo()
        {
            // 更新设备基础信息的逻辑
            if (GridSelectModel == null)
            {
                TipMessage = "请选择要更新的设备";
                return;
            }
            if (string.IsNullOrEmpty(DeviceName) || string.IsNullOrEmpty(DeviceDescription))
            {
                TipMessage = "设备名称和描述不能为空";
                return;
            }
            GridSelectModel.DeviceName = DeviceName;
            GridSelectModel.DeviceDescription = DeviceDescription;
            GridSelectModel.DeviceType = DeviceTypeEnumExtensions.GetDeviceTypeByDescription(SelectDeviceTypeDesc);
            GridSelectModel.DeviceTypeName = SelectDeviceTypeDesc;
            var i = _deviceBaseInfoService.UpdateData(GridSelectModel);
            if (i <= 0)
            {
                TipMessage = "设备更新失败";
                return;
            }
            InitData();
            TipMessage = "设备更新成功";
        }
   
    }
}
